package koction.model.productComment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import koction.model.consumer.ConsumerVO;

import org.springframework.stereotype.Service;

@Service
public class ProductCommentServiceImpl implements ProductCommentService {
	@Resource(name="productCommentDaoImpl")
	private ProductCommentDao productCommentDao;
	
	public int writeProductComment(ProductCommentVO pcvo){
		System.out.println(pcvo);
		 productCommentDao.writeProductComment(pcvo);
		String starRating= productCommentDao.getProductAvgStarRating(pcvo.getProductId());
		int star=0;
		if(starRating==null){
			star=0;
		}else{
			star=Integer.parseInt(starRating);
		}
		Map map=new HashMap();
		map.put("starRating", star);
		map.put("productId", pcvo.getProductId());
		return productCommentDao.updateProductStarRating(map);
	}
	
	public ListVO getProductCommentList(int productId, String pageNo){
		int p=1;
		if(pageNo!=null){
			p=Integer.parseInt(pageNo);
		}
		Map map=new HashMap();
		map.put("productId", productId);
		map.put("pageNo", p);
		List<ProductCommentVO> list=productCommentDao.getProductCommentList(map);
		PagingBean pagingBean=new PagingBean(p,productCommentDao.getProductCommentCount(productId));
		return new ListVO(list,pagingBean);
	}
	public int writeCommentReply(ConsumerVO cvo, int commentNo,String replyInfo){
		ProductCommentVO pcvo=productCommentDao.getCommentInfoByNo(commentNo);
		productCommentDao.updateRestep(pcvo);
		pcvo.setId(cvo.getId());
		pcvo.setCommentInfo(replyInfo);
		pcvo.setRelevel(pcvo.getRelevel()+1);
		pcvo.setRestep(pcvo.getRestep()+1);
		return productCommentDao.writeCommentReply(pcvo);
		
	}
	public int deleteCommentReply(int commentNo){
		ProductCommentVO pcvo=productCommentDao.getCommentInfoByNo(commentNo);
		productCommentDao.deleteCommentReply(commentNo);
		String starRating= productCommentDao.getProductAvgStarRating(pcvo.getProductId());
		int star=0;
		if(starRating==null){
			star=0;
		}else{
			star=Integer.parseInt(starRating);
		}
		Map map=new HashMap();
		map.put("starRating", star);
		map.put("productId", pcvo.getProductId());
		return productCommentDao.updateProductStarRating(map);
	}
	public int modifyComment(int commentNo, String modifyInfo){
		Map map= new HashMap();
		map.put("commentNo", commentNo);
		map.put("modifyInfo",modifyInfo);
		return productCommentDao.modifyComment(map);
	}
	
	
}
